//1658.将x减到0的最小操作数
class Solution {
    public int minOperations(int[] nums, int x) {
        int sum = 0;
        for(int a : nums){
            sum += a;
        }
        int target = sum - x;
        if(target < 0){
            return -1;
        }
        int ret = -1;

        int left = 0;
        int right = 0;
        int tmp = 0;
        for(right = 0;right < nums.length;right++){
            //进窗口
            tmp += nums[right];
            //判断
            while(tmp > target){
                //出窗口
                tmp -= nums[left++];
            }
            if(tmp == target){
                //更新结果
                ret = Math.max(ret,right-left +1);
            }          
        }
        if(ret == -1){
            return ret;
        }else{
            return nums.length -ret;
        }
    }
}